<?php
namespace app\weixin\controller;





use think\Db;
use think\Request;

class Jfmall extends \think\Controller
{
    public function index()
    {

        $ad=Db::table('ad')->where('type','jifen')->select();
        $this->assign('ad',$ad);
        $list=Db::table('jifen_product')->where(['type'=>'ka','state'=>3])->order('rand()')->limit(15)->select();
        $this->assign('kalist',$list);
        return view();


    }

    public function malllist(){

       $where['type']=input('type');
       $where['state']=3;
       $list= Db::table('jifen_product')->where($where)->order('rand()')->select();
       $this->assign('list',$list);


        return view();
    }


    public function details(){
      $where['id']=input('id');
      $t=Db::table('jifen_product')->where($where)->find();
      Db::table('jifen_product')->where($where)->setInc('click');
      $user=Db::table('user')->find(session('uid'));
       if($user['ka']>=$t['price']){

       $ustate=1;
 
       }else{

          $ustate=0;

       }

       $this->assign('ustate',$ustate);

      $this->assign('t',$t);
       return view();
    }

    //购物车订单处理合并付款监测
    public function jforder(){
    $s=input('param.');
    $t=Db::table('jifen_product')->find($s['id']);
     if(IS_AJAX()){
      
      $user=Db::table('user')->find(session('uid'));

      if($user['ka']>=$t['price']){

        $data['uid']=session('uid');
       $data['orderid']="KA".date('YmdHis').mt_rand(111111,999999);
       $data['title']="礼品卡购物 ".$t['name'];
       $data['addtime']=date('Y-m-d H:i:s');
       $data['paytime']=date('Y-m-d H:i:s');
       $data['name']=$s['name'];
       $data['tel']=$s['tel'];
       $data['city']=$s['city'];
       $data['address']=$s['address'];
       $data['type']="ka";
       $data['state']=1;
       $data['money']=$t['price'];
       $data['pic']=$t['pic'];
        Db::table('user')->where('id',session('uid'))->setDec('ka',$t['price']);
        Db::table('so9_order')->insert($data);

                $sdata['addtime']=date('Y-m-d H:i:s');
                $sdata['title']="礼品卡消费";
                $sdata['uid']=session('uid');
                $sdata['type']="-";
                $sdata['price']=$t['price'];
                Db::table('user_money_log')->insert($sdata);
      
       $json['code']=200;




      }else{
        
        $json['code']=500;
        $json['msg']="您的礼品余额不足";


      }

      
       
    return json($json);



     }else{

     

      $where['uid']=session('uid');
      $u=Db::table('so9_order')->where($where)->find();
      $this->assign('u',$u);

       $script = &  load_wechat('Script');
       $qm = $script->getJsSign('http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]);
       $this->assign('qm',$qm);
      $this->assign('u',$u);
      $this->assign('t',$t);
      return view();
     }

    

    }



}
